package networking;


import java.net.*;
import java.io.*;
import java.util.*;


/**
 * 
 * 
 * @author Michael
 */
public class SendeThread implements Runnable{
    static int length = 256;
    DatagramSocket socket;
    Vector clients;
    
    
/**
 * Dieser Thread versendet die kompletten Daten an alle Clients ca. alle 30ms
 * @param socket
 * @param clients 
 */

SendeThread(DatagramSocket socket, Vector clients) {
    this.socket = socket;
    this.clients = clients;
    Thread t = new Thread(this);
    t.start();
}


public void run() {
    //System.out.println("Neuer Threat erstellt: Senden");
    while( true ) {
        try{
            //Alles x Millisekunden wird das Paket gesendet
            Thread.sleep(30);
        }
        catch(Exception e){
            System.err.println("Ausnahmefehler: " + e);
        }
        //An alle Clients Pakete senden
        for(int i=0;i<clients.size();i++){
            //Adresse und Ports setzen
            InetSocketAddress dest = (InetSocketAddress) clients.get(i);
            DatagramPacket packet = new DatagramPacket( Server.getPaket(),Server.getPaket().length, dest.getAddress(), dest.getPort());            
            try{                
                socket.send( packet );
                String text = new String(packet.getData(), 0, packet.getLength());
                //System.out.println("Paket an Spieler " + i + " mit " + text + " gesendet");
            }
            catch( IOException e ) {
                System.err.println("Ausnahmefehler: " + e );
            }            
        }
    }
}
}
